package com.cash.loan.modular.cash.controller;

import com.cash.loan.modular.cash.model.ProductAdCate;
import com.cash.loan.modular.cash.result.ServerResponse;
import com.cash.loan.modular.cash.service.AdCateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @description
 * @date 2018年09月30 18:53
 */
@Controller
@RequestMapping(value = "/cash/adCate")
@Api(description = "广告类型管理")
public class AdCateController {

    private static final Logger log = LoggerFactory.getLogger(AdCateController.class);

    private static final String PREFIX = "/cash/adcate";

    @Autowired
    private AdCateService adCateService;

    /**
     * 广告类型列表页面
     * @return
     */
    @RequestMapping(value = "/listPage")
    public String listPage() {
        return PREFIX + "/ad_cate_list.html";
    }

    @ApiOperation("广告类型列表")
    @GetMapping(value = "/list")
    @ResponseBody
    public ServerResponse list() {
        List<ProductAdCate> productAdCateList = adCateService.list();
        return ServerResponse.getSuccess("success",productAdCateList);
    }

    @ApiOperation("添加广告类型")
    @PostMapping(value = "/add")
    @ResponseBody
    public ServerResponse add(@RequestBody Map<String,String> adCateName) {
        String name = adCateName.get("name");
        if(StringUtils.isBlank(name)) {
            return ServerResponse.getErrorMessage("请输入类型名称");
        }
        Boolean addRes = adCateService.add(name);
        if(addRes) {
            return ServerResponse.getSuccess("添加成功");
        } else {
            return ServerResponse.getErrorMessage("添加失败");
        }
    }

    @ApiOperation("广告类型详情")
    @GetMapping(value = "/detail")
    @ResponseBody
    public ServerResponse detail(@RequestParam(value = "id") Integer id) {
        ProductAdCate productAdCate = adCateService.detail(id);
        return ServerResponse.getSuccess("success",productAdCate);
    }

    @ApiOperation("修改广告类型")
    @PostMapping(value = "/update")
    @ResponseBody
    public ServerResponse update(@RequestBody Map<String,Object> adCate) {
        String name = (String) adCate.get("name");
        if(StringUtils.isBlank(name)) {
            return ServerResponse.getErrorMessage("请填写完整数据");
        }
        Integer id = (Integer) adCate.get("id");
        if(id == null) {
            return ServerResponse.getErrorMessage("请填写修改数据对应的id");
        }
        Boolean updateRes = adCateService.update(name,id);
        if(updateRes) {
            return ServerResponse.getSuccess("修改成功");
        } else {
            return ServerResponse.getErrorMessage("修改失败");
        }
    }

    @ApiOperation("删除类型")
    @PostMapping(value = "/delete")
    @ResponseBody
    public ServerResponse delete(@RequestParam(value = "id") Integer id) {
        Boolean delRes = adCateService.delete(id);
        if(delRes) {
            return ServerResponse.getSuccess("删除成功");
        } else {
            return ServerResponse.getErrorMessage("删除失败");
        }
    }
}
